/**
 * Created by aop on 18-1-4.
 */
layui.use(['form', 'table', 'laypage'], function () {
	var table = layui.table, laypage = layui.laypage, form = layui.form;
	var tmpParams = {};
	var curr = 1;
	var pageNum = 8;
	var content_details = $('#content_details');
	var select_list = $('#select_classify');
	var select_list_add = $('#select_classify_pane');
	//监听工具条
	table.on('tool(blogInfo)', function (obj) {
		var data = obj.data, layEvent = obj.event;
		if (layEvent === 'detail') {

			layer.open({
				type: 1,
				title: '博客详情',
				offset: '0',
				area: ['100%', '100%'],
				resize: false,
				move: false,
				content: $('#content_details')
			});
			content_details.empty();
			content_details.append(
				'<div>' +
				'   <h3 style="text-align: center; margin-bottom: 20px">' + data.title + '</h3>' +
				'   <span style="color: #999999;">' +
				'       阅读量:' + data.pvNum + '' +
				'   </span>' +
				'   <span style="color: #999999; margin-left: 20px;">' +
				'       创建日期：' + data.createTime + '' +
				'   </span>' +
				'   <span style="color: #999999; margin-left: 20px;">' +
				'       作者：' + data.author + '' +
				'   </span>' +
				'   <hr class="layui-bg-green">' +
				'</div>'
			);
			content_details.append(data.content);
		} else if (layEvent === 'del') {
			layer.confirm('确认删除【' + data.title + '】？', {btn: ['确定', '取消'], offset: '10px'}, function () {
				$.ajax({
					type: 'post',
					url: '/blog/del_blog',
					data: {blog_id: data.blog_id},
					dataType: 'json',
					success: function (data) {
						layer.msg(data.msg, {offset: '100px'});
						loadData(tmpParams);
					}
				});
			}, function () {
			})
		}
	});

	loadData(tmpParams);
	function loadData(params) {
		var url = '/blog/blog_list_data';
		params['limit'] = pageNum;
		params['start'] = (curr - 1) * pageNum;

		tmpParams = params;
		$.ajax({
			type: 'post',
			url: url,
			data: tmpParams,
			dataType: 'json',
			success: function (data) {
				table.init('blogInfo', {
					width: 1048,
					data: data.data
				});
				//分页
				laypage.render({
					elem: 'blogPage'
					, count: data.count
					, limit: pageNum
					, curr: curr
					, theme: '#999999'
					, jump: function (obj, first) {
						if (!first) {
							curr = obj.curr;
							loadData(params);
						}
					}
				});
			}
		});
	}

	loadSelectClassify();
	function loadSelectClassify() {
		select_list.empty();
		select_list_add.empty();
		select_list.append("<option value='' selected></option>");
		select_list_add.append("<option value='' selected></option>");
		$.ajax({
			type: 'post',
			url: '/blog/load_blog_classify',
			data: {},
			dataType: 'json',
			success: function (data) {
				$.each(data, function (i, v) {
					select_list.append('<option value="' + i + '">' + v + '</option>');
					select_list_add.append('<option value="' + i + '">' + v + '</option>');
				});
				form.render('select');
			}
		});
	}

	//查询
	$('#query').on('click', function () {
		var title = $('#title').val();
		var classify_id = $('#select_classify').val();
		tmpParams = {title: title, classify_id: classify_id}

		loadData(tmpParams);
	});

	//input 模糊查询
	query = function () {
		var title = $('#title').val();
		tmpParams = {title: title}
		loadData(tmpParams);
	}

	/** 新增弹窗 **/
	$('#add_blog').on('click', function () {
		layer.open({
			type: 1,
			title: '博客新增',
			offset: '0',
			area: ['100%', '100%'],
			resize: false,
			move: false,
			content: $('#add_blog_pane')
		});
	});
});
